package com.nutricia.core.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.nutricia.common.api.request.AddExternalPeopleReq;
import com.nutricia.common.api.request.AddInsidePeopleReq;
import com.nutricia.common.api.result.QueryMeetingEpeople;
import com.nutricia.common.api.result.QueryMeetingIpeople;
import com.nutricia.common.api.result.QueryOptionExternal;
import com.nutricia.common.api.result.QueryOptionExternalByMid;
import com.nutricia.common.api.result.QueryOptionExternalByMidRes;
import com.nutricia.common.api.result.QueryOptionInsideByMid;
import com.nutricia.common.util.CheckUtil;
import com.nutricia.core.ex.BizErrorCode;
import com.nutricia.core.ex.BizException;
import com.nutricia.core.model.domain.ExternalPeopleDO;
import com.nutricia.core.model.domain.ExternalPeopleRelaDO;
import com.nutricia.core.model.domain.InsidePeopleDO;
import com.nutricia.core.model.domain.InsidePeopleRelaDO;
import com.nutricia.core.model.domain.MeetingDO;
import com.nutricia.core.model.domain.OptionDO;
import com.nutricia.core.service.MeetingSignUpService;

@Service("MeetingSignUpService")
public class MeetingSignUpServiceImpl extends AbstractServiceImpl  implements MeetingSignUpService{
	@Override
	public MeetingDO queryMeeting(String mId) {
		logger.info("根据会议ID查询会议信息开始！");
		MeetingDO meetingDO = null;
		try {
			meetingDO = meetingSignUpMapper.queryMeetingByMid(mId);
		} catch (Exception e) {
			logger.error("根据会议id获取会议信息异常：{MeetingSignUpServiceImpl.queryMeeting}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("根据会议ID查询会议信息成功！");
		return meetingDO;
	}

	@Override
	public List<QueryMeetingIpeople> queryMeetingIpeople(String mId) {
		logger.info("根据会议ID查询内部人员参会信息开始！");
		List<InsidePeopleDO> insidePeopleDOList = new ArrayList<InsidePeopleDO>();
		List<QueryMeetingIpeople> queryMeetingIpeopleList = new ArrayList<QueryMeetingIpeople>();
		//日期格式转换成字符串
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			insidePeopleDOList = meetingSignUpMapper.queryMeetingIpeopleByMid(mId);
			//数据库DO转换成前台需要的info
			for(InsidePeopleDO insidePeopleDO : insidePeopleDOList) {
				QueryMeetingIpeople queryMeetingIpeople = new QueryMeetingIpeople();
				queryMeetingIpeople.setInsidePkId(insidePeopleDO.getInsidePkId());
				//queryMeetingIpeople.setPersonnelId(insidePeopleDO.getPersonnelId());
				queryMeetingIpeople.setName(insidePeopleDO.getName());
				queryMeetingIpeople.setLargeArea(insidePeopleDO.getLargeArea());
				queryMeetingIpeople.setTitle(insidePeopleDO.getTitle());
				queryMeetingIpeople.setTripType(insidePeopleDO.getTripType());
				if(insidePeopleDO.getTripTime() != null) 
					queryMeetingIpeople.setTripTime(sdf.format(insidePeopleDO.getTripTime()).toString());
				queryMeetingIpeople.setTripFlightNumber(insidePeopleDO.getTripFlightNumber());
				queryMeetingIpeople.setRtripType(insidePeopleDO.getRtripType());
				if(insidePeopleDO.getRtripTime() != null)
					queryMeetingIpeople.setRtripTime(sdf.format(insidePeopleDO.getRtripTime()).toString());
				queryMeetingIpeople.setRtripFlightNumber(insidePeopleDO.getRtripFlightNumber());
				queryMeetingIpeopleList.add(queryMeetingIpeople);
			}
		} catch (Exception e) {
			logger.error("根据会议id获取参会内部人员信息集合异常：{MeetingSignUpServiceImpl.queryMeetingIpeople}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("根据会议ID查询内部人员参会信息成功！");
		return queryMeetingIpeopleList;
	}

	@Override
	public List<QueryMeetingEpeople> queryMeetingEpeople(String mId) {
		logger.info("根据会议ID查询外部参会客户信息开始！");
		List<ExternalPeopleDO> externalPeopleDOList = new ArrayList<ExternalPeopleDO>();
		List<QueryMeetingEpeople> queryMeetingEpeopleList = new ArrayList<QueryMeetingEpeople>();
		//日期格式转换成字符串
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			externalPeopleDOList = meetingSignUpMapper.queryMeetingEpeopleByMid(mId);
			//数据库DO转换成前台需要的info
			for(ExternalPeopleDO externalPeopleDO : externalPeopleDOList) {
				QueryMeetingEpeople queryMeetingEpeople = new QueryMeetingEpeople();
				queryMeetingEpeople.setExternalPkId((externalPeopleDO.getExternalPkId()));
				//queryMeetingEpeople.setPersonnelId(externalPeopleDO.getPersonnelId());
				queryMeetingEpeople.setCustomerName(externalPeopleDO.getCustomerName());
				queryMeetingEpeople.setCompany(externalPeopleDO.getCompany());
				queryMeetingEpeople.setDepartment(externalPeopleDO.getDepartment());
				queryMeetingEpeople.setRank(externalPeopleDO.getRank());
				queryMeetingEpeople.setTripType(externalPeopleDO.getTripType());
				if(externalPeopleDO.getTripTime() != null)
					queryMeetingEpeople.setTripTime(sdf.format(externalPeopleDO.getTripTime()).toString());
				queryMeetingEpeople.setTripFlightNumber(externalPeopleDO.getTripFlightNumber());
				queryMeetingEpeople.setRtripType(externalPeopleDO.getRtripType());
				if(externalPeopleDO.getRtripTime() != null)
					queryMeetingEpeople.setRtripTime(sdf.format(externalPeopleDO.getRtripTime()).toString());
				queryMeetingEpeople.setRtripFlightNumber(externalPeopleDO.getRtripFlightNumber());
				queryMeetingEpeopleList.add(queryMeetingEpeople);
			}
		} catch (Exception e) {
			logger.error("根据会议id获取参会外部人员信息集合异常：{MeetingSignUpServiceImpl.queryMeetingEpeople}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("根据会议ID查询外部客户参会信息成功！");
		return queryMeetingEpeopleList;
	}

	@Override
	@Transactional
	public String addInsidePeople(AddInsidePeopleReq addInsidePeopleReq) {
		logger.info("新增内部人员参会信息开始！");
		Integer pkId = null;
		//是否重复添加验证
		int count = meetingSignUpMapper.checkMeetingInsidePeople(Integer.parseInt(addInsidePeopleReq.
				getMeetingId()),addInsidePeopleReq.getCaacCertificateCode());
		CheckUtil.isTrue(count == 0, BizErrorCode.MEETING_INSIDE_PEOPLE_EXIST, 
				BizErrorCode.MEETING_INSIDE_PEOPLE_EXIST.getDescription());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			InsidePeopleDO insidePeopleDO = new InsidePeopleDO();
			//insidePeopleDO.setPersonnelId(addInsidePeopleReq.getPersonnelId());
			insidePeopleDO.setMeetingId(Integer.parseInt(addInsidePeopleReq.getMeetingId()));
			insidePeopleDO.setName(addInsidePeopleReq.getName());
			insidePeopleDO.setCaacCertificateCode(addInsidePeopleReq.getCaacCertificateCode());
			insidePeopleDO.setSex(addInsidePeopleReq.getSex());
			insidePeopleDO.setLargeArea(addInsidePeopleReq.getLargeArea());
			insidePeopleDO.setTitle(addInsidePeopleReq.getTitle());
			insidePeopleDO.setTelephone(addInsidePeopleReq.getTelephone());
			insidePeopleDO.setEmail(addInsidePeopleReq.getEmail());
			insidePeopleDO.setTripType(addInsidePeopleReq.getTripType());
			insidePeopleDO.setTripFlightNumber(addInsidePeopleReq.getTripFlightNumber());
			if(addInsidePeopleReq.getTripTime() != null && !"".equals(addInsidePeopleReq.getTripTime())) 
				insidePeopleDO.setTripTime(df.parse(addInsidePeopleReq.getTripTime()));
			
			insidePeopleDO.setTripStartingPoint(addInsidePeopleReq.getTripStartingPoint());
			insidePeopleDO.setTripCostIsSelf(addInsidePeopleReq.getTripCostIsSelf());
			insidePeopleDO.setRtripType(addInsidePeopleReq.getRtripType());
			insidePeopleDO.setRtripFlightNumber(addInsidePeopleReq.getRtripFlightNumber());
			if(addInsidePeopleReq.getRtripTime() != null && !"".equals(addInsidePeopleReq.getRtripTime())) 
				insidePeopleDO.setRtripTime(df.parse(addInsidePeopleReq.getRtripTime()));
			
			insidePeopleDO.setRtripStartingPoint(addInsidePeopleReq.getRtripStartingPoint());
			insidePeopleDO.setRtripCostIsSelf(addInsidePeopleReq.getRtripCostIsSelf());
			insidePeopleDO.setIsGoBackHandle('0');
			insidePeopleDO.setState('1');
			insidePeopleDO.setStateUpdateReason('0');
			insidePeopleDO.setRemarks(addInsidePeopleReq.getRemarks());
			insidePeopleDO.setInviterLargeArea("邀请人大区待获取！");
			insidePeopleDO.setFounderId("创建人id待获取！");
			
			meetingSignUpMapper.addInsidePeople(insidePeopleDO);
			
			//通过会议ID、和参会人员证件号获取新增数据的主键ID
			pkId = meetingSignUpMapper.getIpeoplePkId(addInsidePeopleReq.getMeetingId()+"",
					addInsidePeopleReq.getCaacCertificateCode()+"");
			
			//新增选项
			if(addInsidePeopleReq.getOptions() != null) {
				String [] strs = addInsidePeopleReq.getOptions().split(",");
				for(String str : strs) {
					InsidePeopleRelaDO insidePeopleRelaDO = new InsidePeopleRelaDO();
					insidePeopleRelaDO.setInsidePkId(pkId);
					insidePeopleRelaDO.setOptionId(Integer.parseInt(str));
					insidePeopleRelaDO.setState('1');
					insidePeopleRelaDO.setFounderId("待获取！");
					meetingSignUpMapper.addInsideOption(insidePeopleRelaDO);
				}
			}
			
			//查询航班号表是否有此航班号
			
			
			
			//航班号表没有此航班号，新增航班号到航班号表
			
			
			
		} catch (Exception e) {
			logger.error("新增内部参会人员信息异常：{MeetingSignUpServiceImpl.addInsidePeople}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("新增内部人员参会信息成功！");
		return pkId+"";
	}

	@Override
	public List<QueryOptionInsideByMid> queryOptionInsideByMid(String mId) {
		logger.info("通过会议ID获取会议所有选项开始！");
		List<QueryOptionInsideByMid> queryOptionByMidList = new ArrayList<QueryOptionInsideByMid>();
		try {
			List<OptionDO> optionDOList = meetingSignUpMapper.queryOptionInsideByMid(mId);
			for(OptionDO optionDO : optionDOList) {
				QueryOptionInsideByMid queryOptionByMid = new QueryOptionInsideByMid();
				queryOptionByMid.setmId(optionDO.getmId()+"");
				queryOptionByMid.setOptionId(optionDO.getOptionId()+"");
				queryOptionByMid.setOptionName(optionDO.getOptionName());
				queryOptionByMidList.add(queryOptionByMid);
			}
		} catch (Exception e) {
			logger.error("通过ID获取会议所有选项异常：{MeetingSignUpServiceImpl.queryOptionByMid}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("通过会议ID获取会议所有选项成功！");
		return queryOptionByMidList;
	}

	@Override
	public QueryOptionExternalByMidRes queryOptionContactsByMid(String mId) {
		logger.info("通过会议ID获取会议所有选项和联系人信息开始！");
		QueryOptionExternalByMidRes queryOptionExternalByMidRes = new QueryOptionExternalByMidRes();
		List<QueryOptionExternalByMid> queryOptionExternalByMidList = new ArrayList<QueryOptionExternalByMid>();
		try {
			//获取会议联系人信息
			MeetingDO meetingDO = meetingSignUpMapper.queryContactsByMid(mId);
			QueryOptionExternal queryOptionExternal = new QueryOptionExternal();
			queryOptionExternal.setmId(meetingDO.getmId()+"");
			queryOptionExternal.setContacts(meetingDO.getContacts());
			queryOptionExternal.setContactNumber(meetingDO.getContactNumber());
			queryOptionExternal.setContactEmail(meetingDO.getContactEmail());
			queryOptionExternalByMidRes.setQueryOptionExternal(queryOptionExternal);
			
			//获取会议选项信息
			List<OptionDO> optionDOList = meetingSignUpMapper.queryOptionInsideByMid(mId);
			for(OptionDO optionDO : optionDOList) {
				QueryOptionExternalByMid queryOptionExternalByMid = new QueryOptionExternalByMid();
				queryOptionExternalByMid.setmId(optionDO.getmId()+"");
				queryOptionExternalByMid.setOptionId(optionDO.getOptionId()+"");
				queryOptionExternalByMid.setOptionName(optionDO.getOptionName());
				queryOptionExternalByMidList.add(queryOptionExternalByMid);
			}
			queryOptionExternalByMidRes.setQueryOptionExternalByMidList(queryOptionExternalByMidList);
		} catch (Exception e) {
			logger.error("通过会议ID获取会议所有选项和联系人信息异常：{MeetingSignUpServiceImpl.queryContactsByMid}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("通过会议ID获取会议所有选项和联系人信息成功！");
		return queryOptionExternalByMidRes;
	}

	@Override
	@Transactional
	public String addExternalPeople(AddExternalPeopleReq addExternalPeopleReq) {
		logger.info("新增会议外部参会人员开始！");
		Integer pkId = null;
		//是否重复验证
		int count = meetingSignUpMapper.checkMeetingExternalPeople(Integer.parseInt(addExternalPeopleReq.
				getMeetingId()),addExternalPeopleReq.getCaacCertificateCode());
		CheckUtil.isTrue(count == 0, BizErrorCode.MEETING_EXTERNAL_PEOPLE_EXIST, 
				BizErrorCode.MEETING_EXTERNAL_PEOPLE_EXIST.getDescription());
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			ExternalPeopleDO externalPeopleDO = new ExternalPeopleDO();
			externalPeopleDO.setMeetingId(Integer.parseInt(addExternalPeopleReq.getMeetingId()));
			externalPeopleDO.setCustomerName(addExternalPeopleReq.getCustomerName());
			externalPeopleDO.setCaacCertificateCode(addExternalPeopleReq.getCaacCertificateCode());
			externalPeopleDO.setSex(addExternalPeopleReq.getSex());
			externalPeopleDO.setCompany(addExternalPeopleReq.getCompany());
			externalPeopleDO.setDepartment(addExternalPeopleReq.getDepartment());
			externalPeopleDO.setRank(addExternalPeopleReq.getRank());
			externalPeopleDO.setPositionTitle(addExternalPeopleReq.getPositionTitle());
			externalPeopleDO.setCostomerLevel(addExternalPeopleReq.getCostomerLevel());
			externalPeopleDO.setIsKol(addExternalPeopleReq.getIsKol());
			externalPeopleDO.setIsKdm(addExternalPeopleReq.getIsKdm());
			externalPeopleDO.setTelephone(addExternalPeopleReq.getTelephone());
			externalPeopleDO.setEmail(addExternalPeopleReq.getEmail());
			externalPeopleDO.setTripType(addExternalPeopleReq.getTripType());
			externalPeopleDO.setTripFlightNumber(addExternalPeopleReq.getTripFlightNumber());
			if(addExternalPeopleReq.getTripTime() != null && !"".equals(addExternalPeopleReq.getTripTime()))
				externalPeopleDO.setTripTime(df.parse(addExternalPeopleReq.getTripTime()));
			
			externalPeopleDO.setTripStartingPoint(addExternalPeopleReq.getTripStartingPoint());
			externalPeopleDO.setTripCostIsSelf(addExternalPeopleReq.getTripCostIsSelf());
			externalPeopleDO.setRtripType(addExternalPeopleReq.getRtripType());
			externalPeopleDO.setRtripFlightNumber(addExternalPeopleReq.getRtripFlightNumber());
			if(addExternalPeopleReq.getRtripTime() != null && !"".equals(addExternalPeopleReq.getRtripTime()))
				externalPeopleDO.setRtripTime(df.parse(addExternalPeopleReq.getRtripTime()));
			
			externalPeopleDO.setRtripStartingPoint(addExternalPeopleReq.getRtripStartingPoint());
			externalPeopleDO.setRtripCostIsSelf(addExternalPeopleReq.getRtripCostIsSelf());
			externalPeopleDO.setIsGoBackHandle('0');//去回程状态是否已处理 0-未处理
			externalPeopleDO.setState('1');//状态 1-有效
			externalPeopleDO.setStateUpdateReason('0');//状态修改原因 0-未修改
			externalPeopleDO.setRemarks(addExternalPeopleReq.getRemarks()); 
			externalPeopleDO.setInviterLargeArea("待获取！");
			externalPeopleDO.setFounderId("待获取！");
			
			meetingSignUpMapper.addExternalPeople(externalPeopleDO);
			
			//通过会议ID获取新增会议外部参会人员主键ID用于选项新增
			pkId = meetingSignUpMapper.getEpeoplePkId(addExternalPeopleReq.getMeetingId(), 
					addExternalPeopleReq.getCaacCertificateCode());
			
			//循环新增会议选项
			String[] strs = addExternalPeopleReq.getOptions().split(",");
			for(String str : strs) {
				ExternalPeopleRelaDO externalPeopleRelaDO = new ExternalPeopleRelaDO();
				externalPeopleRelaDO.setExternalPkId(pkId);
				externalPeopleRelaDO.setOptionId(Integer.parseInt(str));
				externalPeopleRelaDO.setState('1');
				externalPeopleRelaDO.setFounderId("待获取！");
				meetingSignUpMapper.addExternalOption(externalPeopleRelaDO);
			}
			
		} catch (Exception e) {
			logger.error("新增会议外部参会人员异常：{MeetingSignUpServiceImpl.addExternalPeople}", e);
	        throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("新增会议外部参会人员成功！");
		return pkId+"";
	}
}
